package com.fairytask.engine.components.autoPullUpList
{
	import com.fairytask.data.BaseData;
	import com.fairytask.engine.components.BaseComponent;
	import com.fairytask.engine.components.Scroll.ScrollPanel;
	import com.fairytask.engine.components.Scroll.ScrollSkin;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.event.AutoPullUpListEvent;
	import com.fairytask.engine.define.Filter;
	import com.fairytask.engine.define.ResConfig;
	import com.fairytask.managers.ResManager;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * 拉升列表项(每个列表项) 
	 * @author zhuy
	 * 
	 */	
	public class AutoPullUpListItem extends BaseComponent
	{	
		public var data			:Array	=	[];
		//属性
		private var _childArr		:Array	=	[];
		public var parentNote		:AutoPullUpListItem;
		private var _currentIcon	:MovieClip;
		private var _rootBgskin	:String; //根项背景str
		public var rootTree		:AutoPullUpList;
		private var _itemName		:String;
		private var _isHidden		:Boolean	=	true;
		//数据
		public var id				:int		=	-1;	//父项的序列
		public var isOpen			:Boolean	=	false;
		private var currentHeight	:int=0; //当前高度
		//显示
		private var _openSkin		:MovieClip;
		private var _closeSkin		:MovieClip;
		private var _sPanel		:ScrollPanel;//显示子项区域滚动条
		private var _childSpace	:Sprite;//Tree子项容器
		private var _iconCkb		:DisplayObject;	//	伸缩/展开按钮
		private var _nameTxt		:TextField;	//父项名称
		private var	_filter		:GlowFilter;
		private var _timeTxt		:TextField;//时间显示
		private var _time			:int;
		private var _icon			:Clip;
		private var _itemRootIcon	:Clip;//根图标
		private var _childIcon		:Clip;//子项图标
		private var _childBg		:Sprite;//子项背景
		private var _mouseRes		:MovieClip;//鼠标移上
		
		private var _layOut		:String = "left";
		private var _bgStr			:String = "";
		private var _isRoot		:Boolean;
		public var hSpacer			:int	=	5
		public var vSpacer			:int	=	10;//子项间隔
		
		public var openData		:BaseData = new BaseData();//对外数据接口
		
		//默认颜色
		private const DEFAULT_COLOR	:int = 0xFFCB4A;//默认颜色
		
		public function AutoPullUpListItem(nameStr:String,_rootItemBg:String="",childBgStr:String="",itemRootIcon:String="",childIconStr:String="",overRes:String="",isRoot:Boolean=false,hideCheck:Boolean=false,rootBgStr:String="",layOut:String="",color:int=DEFAULT_COLOR)
		{
			buttonMode = true;
			if(layOut!="")
			{
				_layOut = layOut;
			}
			
			_mouseRes = ResManager.newResClass(overRes) as MovieClip;
			_mouseRes.y = -2;
			_mouseRes.x = 2;
			
			_isRoot	=	isRoot;
			_rootBgskin	=	_rootItemBg;
			
			var tf		:TextFormat	=	new TextFormat();
			tf.leading	=	2;
			tf.letterSpacing	=	1;
			tf.font	=	"Calibri";
			_nameTxt	=	new TextField();
			_nameTxt.defaultTextFormat		=	tf;
			_nameTxt.mouseWheelEnabled		=	false;
			_nameTxt.selectable		=	false;
			_nameTxt.mouseEnabled	=	true;
			_nameTxt.doubleClickEnabled=true;
			_nameTxt.autoSize	=	TextFieldAutoSize.LEFT;
			_nameTxt.textColor	=	color;
			
			_nameTxt.height	=	20;
			_nameTxt.multiline	=	true;
			_nameTxt.htmlText	=	nameStr;
			_itemName 	=	nameStr;
			
			if(_isRoot)//根项
			{
				_nameTxt.filters	=	[Filter.COMMON_LABEL_FILTER];
				_nameTxt.x=	80;
				_nameTxt.y= 10;
				//根项背景simpleButton按钮
				_iconCkb	=	 ResManager.newResClass(_rootBgskin) as DisplayObject;
				_iconCkb.width	=	195;
				_iconCkb.height	=	40;
				_iconCkb.x	=	1;
				addChild(_iconCkb);
				_itemRootIcon = new Clip();
				_itemRootIcon.source = ResConfig.pngResPath + itemRootIcon;
				_itemRootIcon.x = 4;
				_itemRootIcon.y = 4;
				addChild(_itemRootIcon);
				addChild(_nameTxt);
				_childSpace = new Sprite();
				_sPanel	=	new ScrollPanel(new Rectangle(0,0,190,0),_childSpace,new ScrollSkin("vScrol_1",""));
				_sPanel.y = _iconCkb.height;
				addChild(_sPanel);
			}else//子项
			{
				_childBg = new Sprite();
				var res:MovieClip = ResManager.newResClass(childBgStr) as MovieClip; 
				res.width	=	185;
				res.x	=	2;
				_childBg.addChild(res);
				addChild(_childBg);
				_childBg.x = 2;
				_nameTxt.x=	20;
				_nameTxt.y	=  3;	
				_nameTxt.filters = [Filter.COMMON_TEXT_FILTER];
				_nameTxt.textColor = 0xFFFFFF;
				_nameTxt.htmlText  = nameStr;
				addChild(_nameTxt);
				_childIcon = new Clip();
				_childIcon.source =  ResConfig.pngResPath + childIconStr;
				_childIcon.x = 122;
				_childIcon.y = -4;
				addChild(_childIcon);
			}
			initEvent();
		}
		
		private function initEvent():void
		{
			if(_iconCkb != null)
			{
				addEventListener(MouseEvent.CLICK, onItemRootClick);
			}else
			{
				addEventListener(MouseEvent.ROLL_OVER,onOver);
				addEventListener(MouseEvent.ROLL_OUT,onOut);
				addEventListener(MouseEvent.CLICK, onItemClick);
			}
		}
		
		/**
		 * 点击根项 
		 * @param evt
		 * 
		 */		
		private function onItemRootClick(evt:MouseEvent):void
		{
			if(rootTree != null)
			{
				rootTree.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ICON,this,data));
			}
		}
		
		private function onOver(evt:MouseEvent):void
		{
			if(!contains(_mouseRes))
			{
				if(_childIcon && _childIcon.numChildren>0)
				{
					addChildAt(_mouseRes,this.numChildren - 2);
				}else
				{
					addChild(_mouseRes);
				}
			}
		}
		
		/**
		 * 子项跟新底背景 
		 * @param bgStr
		 * 
		 */		
		public function modifyChildBg(bgStr:String):void
		{
			while(_childBg.numChildren>0)
			{
				_childBg.removeChildAt(0);
			}
			var mc:MovieClip	=	ResManager.newResClass(bgStr) as MovieClip
				mc.x = 3;
				mc.y = 1;
				_nameTxt.x=	20;
				_nameTxt.y	=  1;	
			_childBg.addChild(mc);
		}
		
		/**
		 * 子项跟新图标 
		 * 
		 */		
		public function modifyChildIcon(iconStr:String):void
		{
			_childIcon.clear();
			_childIcon.source = ResConfig.pngResPath + iconStr;
		}
		
		private function onOut(evt:MouseEvent):void
		{
			if(contains(_mouseRes))
			{
				removeChild(_mouseRes);
			}
		}
		
		/**
		 * 时间显示txtfiled设置滤镜 
		 * 
		 */		
		private function timeTxtSetFilter():void
		{
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	6;
			glow.quality			= 	BitmapFilterQuality.LOW;
			_timeTxt.filters		=	[glow];
		}
		
		/**
		 * 点击ICON 
		 * @param evt
		 * 
		 */
//		private function onIconClick(evt:MouseEvent):void
//		{
//			if(rootTree!=null)
//			{
//				rootTree.dispatchEvent(new TreeEvent(TreeEvent.CLICK_ICON,this,data));
//			}
//		}
		
		public function get itemName():String
		{
			return _childSpace.name;
		}
		
		/**
		 * 
		 * @双击内容
		 * 
		 */		
//		private function onDoubleClick(evt:MouseEvent):void
//		{
//			if(rootTree!=null&&this.parentNote!=null)
//			{
//				rootTree.dispatchEvent(new TreeEvent(TreeEvent.DOUBLE_CLICK_ITEM,this,data));
//			}
//		}
		/**
		 * 点击内容 
		 * @param evt
		 */
		private function onItemClick(evt:MouseEvent):void
		{
			if(rootTree!=null)
			{
				rootTree.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ITEM,this,data));
			}
		}
		
		/**
		 * 点击根项背景按钮
		 * @param evt
		 */
		private function onCheckBoxClick(evt:MouseEvent):void
		{
			if(rootTree.curItemRoot != null)
			{
				if(rootTree.curItemRoot != evt.currentTarget)
				{
					_iconCkb.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ICON,this,data));
				}
				else
				{
					_iconCkb.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ICON,this,data));
				}
			}
		}
		
		public function get iconCkb():DisplayObject
		{
			return _iconCkb;
		}
		
		/**
		 * 根项添加子项 
		 * @param str 文字内容
		 * @param rt  总列表
		 * @param _id id
		 * @param dt  数据
		 * @param childBgStr   背景
		 * @param childIconStr 图标
		 * @param overRes  鼠标移上资源
		 * @param hideCheck
		 * @param color
		 * @param layOut
		 * 
		 */		  
		public function addItem(str:String,rt:AutoPullUpList,_id:int,dt:Array,childBgStr:String="",childIconStr:String="",overRes:String="",color:int=DEFAULT_COLOR,layOut:String=""):void
		{
			rootTree	= rt;
			isOpen	=	true;
			var tmpNote:AutoPullUpListItem	=	new AutoPullUpListItem(str,_rootBgskin,childBgStr,"",childIconStr,overRes,false,false,"","",color);
			tmpNote.id	=	_id;
			tmpNote.data	=	dt;
			tmpNote.rootTree	=	rt;
			tmpNote.parentNote	=	this;
			
			_childArr.push(tmpNote);
			tmpNote.y	=	currentHeight;
			if(_layOut == "center")
			{
				tmpNote.nameTxt.x = (tmpNote.rootTree.width - tmpNote.nameTxt.width)/2;
			}
			_childSpace.addChild(tmpNote);
			
			currentHeight	+=	tmpNote.childBg.height	+	vSpacer+2;
		}
		
		/**
		 * 隐藏子项 
		 * 
		 */		
		public function hideChild():void
		{
			if(_iconCkb)
			{
				while(_childSpace.numChildren>0)
				{
					_childSpace.removeChildAt(0);
				}
				_sPanel.hideScrollBar();
				resizeScrollPanel(0);
				_sPanel.update();
				if(_sPanel.parent)
				{
					_sPanel.parent.removeChild(_sPanel);
				}
			}
		}
		
		/**
		 * 显示子项 
		 * 
		 */		
		public function showChild():void
		{
			if(_iconCkb)
			{
				currentHeight = 0;
				for(var i:int=0;i<_childArr.length;i++)
				{
					_childArr[i].y = currentHeight;
					if(_layOut == "center")
					{
						_childArr[i].nameTxt.x = (rootTree.width - _childArr[i].nameTxt.width)/2;
					}
					_childSpace.addChild(_childArr[i]);
					currentHeight	+=	_childArr[i].childBg.height	+	vSpacer;
				}
				if(_sPanel.parent == null)
				{
					addChild(_sPanel);
				}
				_sPanel.update();
			}
		}
		
		public function get childBg():Sprite
		{
			return _childBg;
		}
		
		/**
		 * 取得数据 
		 * @return 
		 * 
		 */
		public function get childArr():Array
		{
			return _childArr;
		}
		/**
		 * 移除树项 
		 * @param item
		 * 
		 */
		public function removeItem(item:AutoPullUpListItem):void
		{
//			if(item.parent!=null)
//			{
				_childArr.splice(_childArr.indexOf(item),1);
				//没有子项时
				if(_childArr.length==0)
				{
//					_rootBtn.selected	=	false;	
				}
				formatItem();
//			}
		}
		
		/**
		 * 重新排版树内容 
		 */
		public function formatItem():void
		{
			while(_childSpace.numChildren>0)
			{
				_childSpace.removeChildAt(0);
			}
			currentHeight	=	18;
			
			for(var i:int=0;i<_childArr.length;i++)
			{
				_childSpace.addChild(_childArr[i]);
				_childArr[i].y = currentHeight;
				currentHeight	+=	_childArr[i].height
			}	
		}
		
		/**
		 * 检测是否有这个项 
		 * @param item
		 * @return 
		 * 
		 */
		public function hasItem(item:AutoPullUpListItem):Boolean
		{
			return _childSpace.contains(item);
		}
		
		/**
		 * 子项容器 
		 * @return 
		 * 
		 */		
		public function get childSpace():Sprite
		{
			return _childSpace;
		}
		
		/**
		 * 根据ID取得树项 
		 * @param _id
		 * @return 
		 * 
		 */
		public function getItemById(_id:int):AutoPullUpListItem
		{
			for each(var item:AutoPullUpListItem in _childArr)
			{
				if(item.id	== _id)
				{
					return item;					
				}
			}
			return null;
		}
		/**
		 * 根据ID删除树内容 
		 * @param _id
		 * 
		 */
		public function removeItemById(_id:int):void
		{
			var tmpItem:AutoPullUpListItem	=	getItemById(_id);
			if(tmpItem!=null)
			{
				removeItem(tmpItem);
			}
		}
		
		/**
		 * 根据索引号来选择子项 
		 * @param pIdx 子项索引号
		 * 
		 */
		public function selectChildByIndex(pIdx:int):void
		{
			if(pIdx<_childArr.length&&pIdx>-1)
			{
				var tmpItem	:AutoPullUpListItem	=	_childArr[pIdx];
				if(rootTree!=null&&null!=tmpItem)
				{
					rootTree.dispatchEvent(new AutoPullUpListEvent(AutoPullUpListEvent.AUTO_CLICK_ITEM,tmpItem,tmpItem.data));
				}
			}
		}
		
		/**
		 * 刷新滚动面板 
		 * @param _height
		 * @param _width
		 * 
		 */
		public function resizeScrollPanel(_height:int,_width:int=0):void
		{
			if(_iconCkb)
			{
				if(_width == 0)
				{
					_sPanel.resize(new Rectangle(0,0,_iconCkb.width,_height));
				}else
				{
					_sPanel.resize(new Rectangle(0,0,_width,_height));	
				}
			}
		}
		
		public function get sPanel():ScrollPanel
		{
			return _sPanel;
		}
		
		public function set htmlText(value:String):void
		{
			_nameTxt.htmlText	=	value;			
		}
		
		public function get htmlText():String
		{
			return _nameTxt.htmlText; 
		}
		
		public function get nameTxt():TextField
		{
			return _nameTxt;
		}
		
		public function get isHidden():Boolean
		{
			return _isHidden;
		}
		
		public function set isHidden(value:Boolean):void
		{
			_isHidden = value;
		}
		
		
	}
}